/*
 * Created on Oct 9, 2003
 * LangString
 * Local Projects Database
 * (c) 2003 eRomania Gateway Association
 *
 */
package ro.gateway.aida.obj;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;

import ro.gateway.aida.servlet.Constants;
import ro.gateway.aida.utils.Localizable;


/**
 * @author Mihai Postelnicu
 * LangString
 * This holds a multilingual string
 *
 */
public class LangString implements Localizable, Viewable {
    protected String deflang; //when a requested translation is missing, the translation in this language is searched
    protected String viewLang;
    protected HashMap strings;
    protected int maxViewLength;

    /**
    * @param deflang when a translation in a specific language is missing, the default lang
    * translation will be displayed
    */
    public LangString(String deflang) {
        this.deflang = deflang;
        this.strings = new HashMap();
        this.viewLang = deflang;
        maxViewLength = 0;
    }

    /**
         * Generates the LangString using the default language defined as system property
         */
    public LangString() {
        this.deflang = Constants.DEFAULT_LANGUAGE;
        this.strings = new HashMap();
        viewLang = deflang;
        maxViewLength = 0;
    }

    /**
     * clears the inner hashmap
     *
     */
    public void clear() {
        strings.clear();
    }

    /**
         * @param lang Fetches the translation in the language defined by lang. If
         * the translation is not available it will return the default language
         * @return
         */
    public String getString(String lang) {
        String ret = getFullString(lang);
        if(ret==null) ret="";
        if (maxViewLength == 0)
            return ret;

        if (maxViewLength >= ret.length())
            return ret;

        return ret.substring(0, maxViewLength - 3) + ("...");
    }

    public String getFullString(String lang) {
        if (strings.size() == 0)
            return new String("");

        if (strings.get(lang) != null)
            return (String) strings.get(lang);

        if (strings.get(deflang) != null)
            return (String) strings.get(deflang) + " [" + deflang + "]";

        Iterator i = strings.keySet().iterator();

        if (i.hasNext()) {
            String l = (String) i.next();

            return (String) strings.get(l) + " [" + l + "]";
        } else

            return new String("");
    }

    /**
     * Creates a String representation of this multistring, filtering it with the current view language
     */
    public String toString() {
        return getString(viewLang);
    }

    /**
         * @param lang the language for the new translation
         * @param name the new translation for this LangString
         */
    public void setString(String lang, String name) {
        if (lang == null)
            return;

        strings.put(lang, name);
    }

    /**
     * @return the HasMap containing the available languages as keys and the translations as values
     */
    public HashMap getStrings() {
        return strings;
    }

    /**
     * @param map the HasMap containing the available languages as keys and the translations as values
     */
    public void setStrings(HashMap map) {
        strings = map;
    }

    /**
     * @param map the Hashtable containing the available languages as keys and the translations as values
     */
    public void setStrings(Hashtable map) {
        strings.putAll(map);
    }

    /**
         * @return an Iterator containing the languages available for this LangString
         */
    public Iterator languages() {
        return strings.keySet().iterator();
    }

    /**
     * @return the current view language
     */
    public String getViewLang() {
        return viewLang;
    }

    /**
     * @param string the new view language to be used to render this LangString
     */
    public void setViewLang(String string) {
        viewLang = string;
    }

    /**
     * @return
     */
    public int getMaxViewLength() {
        return maxViewLength;
    }

    /**
     * @param i
     */
    public void setMaxViewLength(int i) {
        maxViewLength = i;
    }
}
